import wx
import wx.lib.intctrl
from wx.lib import stattext
import os
import ConfigParser

_tileSize = wx.Size(30, 20)
_tilePoint = wx.Point(_tileSize.GetWidth(), _tileSize.GetHeight())
_text = """Nag controls
Select the position, linger, and font of your nags.
"""
class Tile(wx.Window):
    def __init__(self, parent, pos, name):
        wx.Window.__init__(self, parent=parent, name = name, size=_tileSize, pos = pos, style=wx.BORDER_SUNKEN)
        self.SetBackgroundColour(wx.NamedColour('white') )

_defaults = dict([('popupposition', '3'), 
    ('popuplinger', '2500'),
    ('fontcolor', '#000000'),
    ('fontdesc', '0;-11;0;0;0;400;0;0;0;0;0;0;0;0;MS Shell Dlg 2'),
    ] )

class NagControlFrame(wx.Frame):
    def __init__(self, config):
        wx.Frame.__init__(self, None, title = "Nag Preferences", style=wx.DEFAULT_FRAME_STYLE)
        self.Config = config
        
        panel = wx.Panel(self)
        
        fivebox = self.fivebox= wx.Panel(panel, size=_tileSize.Scale(1, 1), style=wx.BORDER_SIMPLE)
        fivebox.SetBackgroundColour(wx.NamedColour('dark grey') )
        ul = Tile(fivebox,  pos = (0, 0), name="Upper Left")
        ur = Tile(fivebox, pos = (_tilePoint.x, 0), name="Upper Right")
        ll = Tile(fivebox, pos = (0, _tilePoint.y), name = "Lower Left")
        lr = Tile(fivebox, pos = _tilePoint, name="Lower Right")
        #~ c = Tile(fivebox, pos = (30, 20), name="center")
        
        self.Corner = wx.StaticText(panel, label="", style=wx.ALIGN_CENTRE | wx.ST_NO_AUTORESIZE)
        
        #~ self.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindow, fivebox)
        ul.Bind(wx.EVT_LEFT_UP, self.OnCornerClick)
        ll.Bind(wx.EVT_LEFT_UP, self.OnCornerClick)
        ur.Bind(wx.EVT_LEFT_UP, self.OnCornerClick)
        lr.Bind(wx.EVT_LEFT_UP, self.OnCornerClick)
        #~ c.Bind(wx.EVT_LEFT_UP, self.OnEnterWindow)
        self.subpanels = [ul, ll, ur, lr]
        
        posbox = wx.StaticBox(panel, label="Popup Position")
        psizer= wx.StaticBoxSizer(posbox, wx.VERTICAL)
        psizer.Add(fivebox, 0, wx.ALL, 10)
        psizer.Add(self.Corner, 0, wx.EXPAND | wx.BOTTOM, 10)
        
        lingerbox = wx.StaticBox(panel, label="Popup Linger")
        lsizer = wx.StaticBoxSizer(lingerbox, wx.VERTICAL)
        lingertext = wx.StaticText(panel, label="Enter the linger time in milliseconds")
        self.lingerctrl = wx.lib.intctrl.IntCtrl(panel, min = 500, value=500, limited=True, style=wx.TE_RIGHT)
        self.lingerctrl.SetValue(self.Config.getint('DEFAULT', 'popuplinger') )
        
        lsizer.Add(lingertext, 0, wx.ALL, 5)
        lsizer.Add(self.lingerctrl, 0, wx.ALL, 5)
        
        fontbutton = wx.Button(panel, label="Set Font")
        fontbutton.Bind(wx.EVT_BUTTON, self.OnFontSelection)
        self.fontcolour = wx.BLACK
        
        self.fontsample = wx.StaticText(panel, label = "Nag Text", 
            style= wx.ALIGN_CENTRE | wx.ST_NO_AUTORESIZE) 
        self.fontsample.SetBackgroundColour(wx.WHITE)

        sizer = wx.BoxSizer(wx.VERTICAL)
        text = wx.StaticText(panel, label=_text)
        
        sizer.Add(text, 0, wx.ALL, 5)
        row = wx.BoxSizer(wx.HORIZONTAL)
        row.Add(psizer, 0, wx.ALL, 5)
        row.Add(lsizer, 0, wx.ALL, 5)
        sizer.Add(row)
        
        row = wx.BoxSizer(wx.HORIZONTAL)
        row.Add(fontbutton, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5)
        row.Add(self.fontsample, 1, wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.GROW, 5)
        sizer.Add(row, 0, wx.EXPAND | wx.GROW)
        
        #~ self.fontpicker = wx.FontPickerCtrl(panel, style=wx.FNTP_USE_TEXTCTRL )
        #~ self.fontpicker.Bind(wx.EVT_FONTPICKER_CHANGED, self.OnFont)
        
        #~ sizer.Add(self.fontpicker, 0, wx.EXPAND | wx.GROW | wx.ALL, 5)
    
        
        panel.SetSizer(sizer)
        
        self.Layout()
    
    def OnFont(self, event):
        event.skip()
    
    def OnFontSelection(self, event):
        data = wx.FontData()
        data.EnableEffects(True)
        data.SetColour(wx.Colour().SetFromString(self.Config.get('DEFAULT','fontcolor') ) )
        data.SetInitialFont(wx.FontFromNativeInfoString(self.Config.get('DEFAULT', 'fontdesc') ) )
        
        dlg = wx.FontDialog(self, data)
        
        if dlg.ShowModal() == wx.ID_OK:
            data = dlg.GetFontData()
            font = data.GetChosenFont()
            colour = data.GetColour()
            
            self.fontsample.SetFont(font)
            self.fontsample.SetForegroundColour(colour)
            
            self.fontsample.Refresh()
            
            self.Config.set('DEFAULT', 'fontcolor', colour.GetAsString(wx.C2S_HTML_SYNTAX) )
            self.Config.set('DEFAULT', 'fontdesc', font.GetNativeFontInfoDesc())
            
        
        
    
    def ResetAll(self):
        for p in self.subpanels:
            p.SetBackgroundColour(wx.NamedColour('white') )
    
    def OnCornerClick(self, event):
        print "click", event.GetEventObject().Name
        self.ResetAll()
        event.GetEventObject().SetBackgroundColour(wx.NamedColour('blue') )
        self.fivebox.Refresh()
        self.Corner.SetLabel(event.GetEventObject().Name)
        self.Corner.Refresh()
        


class App(wx.App):
    def OnInit(self):
        self.SetAppName('Nag')
        self.Config = ConfigParser.SafeConfigParser(_defaults)
        self.Config.read(['nag.cfg'])
        
        frame = NagControlFrame(self.Config)
        self.SetTopWindow( frame )
        frame.Show()
        return True

app = App(0)
app.MainLoop()
app.Destroy()
